package cn.ytr.crm.service.impl;

import cn.ytr.crm.domain.Customer;
import cn.ytr.crm.domain.CustomerTransfer;
import cn.ytr.crm.domain.Employee;
import cn.ytr.crm.mapper.CustomerMapper;
import cn.ytr.crm.mapper.CustomerTransferMapper;
import cn.ytr.crm.query.QueryObject;
import cn.ytr.crm.service.ICustomerTransferService;
import cn.ytr.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CustomerTransferServiceImpl implements ICustomerTransferService{

	@Autowired
	private CustomerTransferMapper customerTransferMapper;
	@Autowired
	private CustomerMapper customerMapper;

	/**
	 * 增加或保存客户移交记录
	 */
	@Override
	public void saveOrUpate(CustomerTransfer entity) {
		//操作人为当前登录员工
		Employee employee = UserContext.getCurrentUserByShiro();
		entity.setOperator(employee);
		//操作时间为当前时间
		entity.setOperateTime(new Date());
		if (entity.getId() != null) {
			customerTransferMapper.updateByPrimaryKey(entity);
		} else {
			customerTransferMapper.insert(entity);
		}
		//在客户表里，要把对应的新销售员替换掉旧销售员
		Customer customer = new Customer();
		customer.setId(entity.getCustomer().getId());
		customer.setSeller(entity.getNewSeller());
		customerMapper.updateByPrimaryKey(customer);
	}

	/**
	 * 删除客户移交记录
	 */
	@Override
	public void delete(Long id) {
		customerTransferMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 获取单个客户移交记录
	 */
	@Override
	@Transactional(readOnly=true)
	public CustomerTransfer get(Long id) {
		return customerTransferMapper.selectByPrimaryKey(id);
	}

	/**
	 * 获取所有客户移交记录
	 */
	@Override
	@Transactional(readOnly=true)
	public List<CustomerTransfer> listAll() {
		return customerTransferMapper.selectAll();
	}

	/**
	 * 高级查询所有客户移交记录
	 */
	@Override
	@Transactional(readOnly=true)
	public PageInfo query(QueryObject qo) {
		/*Integer count = CustomerTransferMapper.selectForCount(qo);
		if (count < 1 || count == null) {
			return new PageResult(qo.getPageSize());
		} else {
			List<CustomerTransfer> list = CustomerTransferMapper.selectForList(qo);
			return new PageResult(qo.getCurrentPage(), count, list, qo.getPageSize());
		}*/

		//改用分页插件 pageHelper
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		List<CustomerTransfer> list = customerTransferMapper.selectForList(qo);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;

	}

	/**
	 * 吸纳客户为自己潜在客户
	 */
	public void absorb(CustomerTransfer entity) {
		//吸纳给自己，新销售员为当前操作者也就是当前登录员工
		Employee employee = UserContext.getCurrentUserByShiro();
		entity.setNewSeller(employee);
		entity.setOperator(employee);
		entity.setOperateTime(new Date());

		//还要把客户的销售员改成自己
		Customer customer = entity.getCustomer();
		customer.setSeller(employee);
		//把客户的状态改为潜在客户
		customer.setStatus(Customer.STATUS_COMMON);
		customerMapper.updateByPrimaryKey(customer);
		customerTransferMapper.insert(entity);
	}


}








































